package com.sqxgg.drugstore.job.task;

import com.sqxgg.drugstore.weatherReport.entity.Forecast;
import com.sqxgg.drugstore.weatherReport.entity.WeatherResponse;
import com.sqxgg.drugstore.weatherReport.service.WeatherDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class MyScheduledTask {


    @Autowired
    private WeatherDataService weatherDataService;
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    JavaMailSenderImpl javaMailSender;
    // 上午 10：15   *0 15 10 ? * *  每五秒  */5 * * * * ?
    @Scheduled(cron="1 50 7 ? * *")

    public void task1() throws ParseException, MessagingException {

        logger.info("开始定时任务 邮件发送");
//        WeatherResponse weatherResponse = weatherDataService.getDataByCityName("珠海");
//        List<Forecast> forecast= weatherResponse.getData().getForecast();
//        Forecast today = forecast.get(0);
//        StringBuilder sb = new StringBuilder();
//        sb.append("<p style=\"color:red\">珠海<p/>今天是").append(today.getDate()).append(",最高温度").append(today.getHigh())
//                .append(",最低温度").append(today.getLow()).append(",").append(today.getType())
//                .append(today.getFengxiang()).append(today.getFengli().replace("<![CDATA[","").replace("]]>",""))
//                .append(weatherResponse.getData().getGanmao());
//        today = forecast.get(1);
//        sb.append("<br/>").append("<p style=\"color:red\">珠海<p/>明天是").append(today.getDate()).append(",最高温度").append(today.getHigh())
//                .append(",最低温度").append(today.getLow()).append(",").append(today.getType())
//                .append(today.getFengxiang()).append(today.getFengli().replace("<![CDATA[","").replace("]]>",""))
//                .append(weatherResponse.getData().getGanmao());

        WeatherResponse weatherResponseSY = weatherDataService.getDataByCityName("沈阳");
        List<Forecast> forecastSY= weatherResponseSY.getData().getForecast();
        Forecast todaySY = forecastSY.get(0);
        StringBuilder sbSY = new StringBuilder();
        sbSY.append("<p style=\"color:blue\">沈阳<p/>今天是").append(todaySY.getDate()).append(",最高温度").append(todaySY.getHigh())
                .append(",最低温度").append(todaySY.getLow()).append(",").append(todaySY.getType())
                .append(todaySY.getFengxiang()).append(todaySY.getFengli().replace("<![CDATA[","").replace("]]>",""))
                .append(weatherResponseSY.getData().getGanmao());
        todaySY = forecastSY.get(1);
        sbSY.append("<br/>").append("<p style=\"color:blue\">沈阳<p/>明天是").append(todaySY.getDate()).append(",最高温度").append(todaySY.getHigh())
                .append(",最低温度").append(todaySY.getLow()).append(",").append(todaySY.getType())
                .append(todaySY.getFengxiang()).append(todaySY.getFengli().replace("<![CDATA[","").replace("]]>",""))
                .append(weatherResponseSY.getData().getGanmao());


        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd" );
        Date date = sdf.parse( " 2020-11-01" );
        String result = getDatePoor(date,new Date());
    //    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper simpleMailMessage = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        simpleMailMessage.setSubject("考试倒计时！");

        mimeMessage.setContent("距离考试还有"+result+"别忘记学习哦"+"<br/><p style=\"color:blue\">今明两天沈阳的天气预报<p/>"+sbSY.toString(),"text/html;charset=GBK");
       // simpleMailMessage.setText("距离考试还有"+result+"别忘记学习哦！ \n \u3000 今明两天的天气预报\n"+sb.toString());
        simpleMailMessage.setFrom("MyDreamChangeWorld@163.com");
        // To: 收件人（可以增加多个收件人、抄送、密送）
        InternetAddress[] sendTo = InternetAddress.parse("523419760@qq.com,462428584@qq.com");
        simpleMailMessage.setTo(sendTo);
        //523419760@qq.com;
        //simpleMailMessage.setCc("462428584@qq.com");
        try {
            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            logger.info("结束定时任务 邮件发送失败原因是"+e.getMessage());
            System.out.println(e.getMessage());
        }
        logger.info("结束定时任务 邮件发送");
    }

    public  String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
//        // 计算差多少小时
//        long hour = diff % nd / nh;
//        // 计算差多少分钟
//        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天";
    }
}
